'use strict';

var gulp = require('gulp');
var rev = require('gulp-rev');
var revCollector = require("gulp-rev-collector");
var gulpif = require('gulp-if');
var cleanCSS = require('gulp-clean-css');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var configs = require('../config');
var config = configs.css;
var env = require('../utils/env');
var handleErrors = require('../utils/handleErrors');

// css压缩,生成MD5、sourcemap，并自动补齐浏览器兼容样式
gulp.task('css', function () {
    return gulp.src(config.src).pipe(gulpif(env() === 'development', sourcemaps.init())).pipe(autoprefixer({
        browsers: [
            'last 2 versions', 'Android >= 4.0'
        ],
        cascade: false
    })).pipe(cleanCSS(config.settings)).pipe(rev()).pipe(gulp.dest(config.dest)).pipe(gulpif(env() === 'development', sourcemaps.write('.', {includeContent: false}))).pipe(gulpif(env() === 'development', gulp.dest(config.dest))).pipe(rev.manifest()).on('error', handleErrors).pipe(gulp.dest(config.dest));
});

gulp.task('rep', function () {
    return gulp.src([configs.images.dest + '/*.json', config.dest + '/**/*.css']).pipe(revCollector({replaceReved: true,})).pipe(gulp.dest(config.dest));
});